package org.example.utils;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 在 mybatis-plus中获取 SqlSession的方式不一样了。
 */
public class SqlSessionUtils {
    private static SqlSessionFactory factory;

    static {
        try {
            InputStream is = new FileInputStream(Resources.getResourceAsFile("mybatis-config.xml"));
            // 获取到SqlSessinFactory
            factory = new MybatisSqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        return factory.openSession(); //获取一个SqlSession
    }
}
